const db = require('../db/dbs');
const mysql = require('mysql');
module.exports = {
    // 获取用户持有的花朵总数量
    async getFlowerQuantityAndRecordIDs(UID) {
        const selectQuery = `SELECT COUNT(*) AS flowerCount, GROUP_CONCAT(RecordID) AS recordIDs FROM garden WHERE UID = ?`;
        try {
            const rows = await db.query(selectQuery, [UID]);
            const result = {
                flowerCount: rows[0].flowerCount,
                recordIDs: rows[0].recordIDs ? rows[0].recordIDs.split(',') : []
            };
            return result;
        } catch (error) {
            throw  new Error(`无法获取用户的花朵数据: ${error.message}`);
        }
    },

    // 通过 RecordID 获取 EXP
    async getExpByRecordID(recordID) {
        const selectQuery = `SELECT EXP FROM garden WHERE RecordID = ?`;
        try {
            const rows = await db.query(selectQuery, [recordID]);
            if (rows.length > 0) {
                return rows[0].EXP;
            } else {
                throw new Error(`找不到RecordID为 ${recordID} 的EXP数据`);
            }
        } catch (error) {
            throw new Error(`无法获取EXP数据: ${error.message}`);
        }
    },
    // 通过 RecordID 获取FlowerID
    async getFlowerIDByRecordID(recordID) {
        const selectQuery = `SELECT FlowerID FROM garden WHERE RecordID = ?`;
        try {
            const rows = await db.query(selectQuery, [recordID]);
            if (rows.length > 0) {
                return rows[0].FlowerID;
            } else {
                throw new Error(`找不到RecordID为 ${recordID} 的FlowerID`);
            }
        } catch (error) {
            throw new Error(`无法获取FlowerID: ${error.message}`);
        }
    },
    // 通过RecordID修改花园数据
    async updateGarden(dataType, RecordID, newData) {
        const updateQuery = `UPDATE garden SET ${dataType} = ? WHERE RecordID = ?`;
        console.log(`修改结果: ${updateQuery}`);
        try {
            const result = await db.query(updateQuery, [newData, RecordID]);
            if (result.affectedRows > 0) {
                console.log(`成功更新 RecordID 为 ${RecordID} 的 ${dataType} 数据为 ${newData}`);
                return true; // 修改成功
            } else {
                console.error(`找不到RecordID为 ${RecordID} 的记录或没有更新 ${dataType} 数据`);
                throw new Error(`找不到RecordID为 ${RecordID} 的记录或没有更新 ${dataType} 数据`);
            }
        } catch (error) {
            console.error(`更新花园数据时出错: ${error.message}`);
            throw new Error(`无法更新花园数据: ${error.message}`);
        }
    },
    // 添加随机 FlowerID 和固定 EXP 的新条目
    async addRandomFlower(UID) {
        const FlowerID = (Math.floor(Math.random() * 6) + 1) * 10 + 1; // 随机生成 11 到 61 的 FlowerID
        const Exp = 0; // 固定 EXP 为 0
        const insertQuery = `INSERT INTO garden (UID, FlowerID, EXP) VALUES (?, ?, ?)`;
        try {
            const result = await db.query(insertQuery, [UID, FlowerID, Exp]);
            if (result.affectedRows > 0) {
                console.log(`成功添加新的花园数据：UID ${UID}, FlowerID ${FlowerID}, EXP ${Exp}`);
                return true; // 添加成功
            } else {
                console.error(`添加花园数据失败：UID ${UID}, FlowerID ${FlowerID}, EXP ${Exp}`);
                throw new Error(`添加花园数据失败：UID ${UID}, FlowerID ${FlowerID}, EXP ${Exp}`);
            }
        } catch (error) {
            console.error(`添加花园数据时出错: ${error.message}`);
            throw new Error(`无法添加花园数据: ${error.message}`);
        }
    }
};
